package com.hong.service_cart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hong.fy_common_commomutil.common.result.Result;
import com.hong.fy_model.model.cart.CartEntity;
import com.hong.fy_model.model.cart.ProductCartRelationEntity;
import com.hong.fy_model.model.product.ProductEntity;
import com.hong.fy_model.model.user.UserEntity;
import com.hong.fy_model.vo.cart.CartVo;
import com.hong.service_cart.service.ProductCartRelationService;
import com.hong.service_cart.mapper.ProductCartRelationMapper;
import com.hong.service_product_client.ProductFeignClient;
import com.hong.service_user_client.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("productCartRelationService")
public class ProductCartRelationServiceImpl extends ServiceImpl<ProductCartRelationMapper, ProductCartRelationEntity> implements ProductCartRelationService {

    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private ProductFeignClient productFeignClient;

    @CacheEvict(cacheNames = "productCartRelation", key = "#cartVo.cartId")
    @Override
    public Map<String, String> addProductRE(CartVo cartVo) {
        Map<String, String> map = new HashMap();
        //获取用户ID
        Integer userId = cartVo.getUserId();
        if (userId == null) {
            map.put("code", "201");
            map.put("msg", "用户ID不能为空");
            return map;
        }

        //查询该物品是否已经在购物车中，若存在，则数量，若不存在，则新增一条记录
        LambdaQueryWrapper<ProductCartRelationEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProductCartRelationEntity::getProductId, cartVo.getProductId())
                    .eq(ProductCartRelationEntity::getCartId, cartVo.getCartId());
        ProductCartRelationEntity productCartRelationEntity = this.getOne(queryWrapper);
        if (null != productCartRelationEntity) {
            //若存在，则数量更新
            productCartRelationEntity.setProductNum(productCartRelationEntity.getProductNum() + cartVo.getProductNum());
            this.update(productCartRelationEntity, queryWrapper);
        } else {
            //若不存在，则新增一条记录
            productCartRelationEntity = new ProductCartRelationEntity();
            productCartRelationEntity.setUserId(cartVo.getUserId());
            productCartRelationEntity.setProductId(cartVo.getProductId());
            productCartRelationEntity.setProductNum(cartVo.getProductNum());
            productCartRelationEntity.setCartId(cartVo.getCartId());

            this.save(productCartRelationEntity);

        }
        map.put("code", "200");
        map.put("msg", "添加成功");
        return map;
        }

    @CachePut(cacheNames = "productCartRelation", key = "#cartEntity.cartId")
    @Override
    public List<ProductCartRelationEntity> getAllList(CartEntity cartEntity) {

        //根据购物车ID查询所有数据
        int cartId = cartEntity.getCartId();
        LambdaQueryWrapper<ProductCartRelationEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProductCartRelationEntity::getCartId, cartId);
        List<ProductCartRelationEntity> list = this.list(queryWrapper);

        //遍历list，根据商品ID查询商品信息并且存入parm中
        for (ProductCartRelationEntity productCartRelationEntity : list) {
            Integer productId = productCartRelationEntity.getProductId();
            ProductEntity productEntity = productFeignClient.getById(productId);
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("data", productEntity);
            productCartRelationEntity.setParam(map1);
        }
        return list;
    }
    @CacheEvict(cacheNames = "productCartRelation", key = "#cartVo.cartId")
    @Override
    public Boolean deleteProductRe(CartVo cartVo) {
        LambdaQueryWrapper<ProductCartRelationEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProductCartRelationEntity::getProductId, cartVo.getProductId())
                .eq(ProductCartRelationEntity::getCartId, cartVo.getCartId());
        //查询记录，判断其productNum是否大于1，若大于1，则减1，若等于1，则删除记录
        ProductCartRelationEntity productCartRelationEntity = this.getOne(queryWrapper);
        if (productCartRelationEntity.getProductNum() > 1) {
            productCartRelationEntity.setProductNum(productCartRelationEntity.getProductNum() - 1);
            boolean b = this.update(productCartRelationEntity, queryWrapper);
            return b;
        } else {
            return this.remove(queryWrapper);
        }

    }


}